var json_data = `
{
    "records": [
        {
            "departmentName": "产品部",
            "resDepartmentId": 44,
            "place": "string",
            "userId": "1,1",
            "checkUserNames": "安瑞思,安瑞思",
            "accomplishCount": 2,
            "count": 2,
            "timelyCount": 2
        },
        {
            "departmentName": "产品部",
            "resDepartmentId": 44,
            "place": "啊",
            "userId": "1",
            "checkUserNames": "安瑞思",
            "accomplishCount": 0,
            "count": 1,
            "timelyCount": 1
        },
        {
            "departmentName": "产品部",
            "resDepartmentId": 44,
            "place": "的",
            "userId": "1",
            "checkUserNames": "安瑞思",
            "accomplishCount": 1,
            "count": 1,
            "timelyCount": 1
        }
    ]
}`;

var data = JSON.parse(json_data);
console.log(data.records); // 这就是一个JavaScript对象数组

//两个数相除，保留两位小数
function division(a, b) {
  if (b == 0) {
    return 0;
  }
  return Math.round((a / b) * 100) / 100;
}
division(1, 3);

// ((item.timelyCount / item.count) * 100).toFixed(1);将这个改为方法
// 保留一位小数
function toFixed1(a, b) {
  if (b == 0) {
    return 0;
  }
  return ((a / b) * 100).toFixed(1);
}
toFixed1(1, 3); // 33.3

// 保留两位小数
function toFixed2(a, b) {
  if (b == 0) {
    return 0;
  }
  return ((a / b) * 100).toFixed(2);
}

toFixed2(1, 3); // 33.33

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
//对数组进行降序排序
arr.sort((a, b) => b - a);
console.log(arr); // [9, 8, 7, 6, 5, 4, 3, 2, 1]
//对数组进行升序排序
arr.sort((a, b) => a - b);
console.log(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

//对后端数据进行处理,处理成el-tree组件需要的数据格式
let data = [
  { id: 1, name: "a", parentId: 0, categoryId: 1, categoryName: "产品部" },
  { id: 2, name: "b", parentId: 0, categoryId: 2, categoryName: "技术部" },
  { id: 3, name: "c", parentId: 1, categoryId: 1, categoryName: "产品部" },
  { id: 4, name: "d", parentId: 1, categoryId: 2, categoryName: "技术部" },
];
//将上面的数据处理成eltree组件需要的数据格式,将categoryName的数据作为根节点,将name的数据作为子节点子节点是根据categoryName进行分组的,一样的categoryName的数据就是一个子节点
let result = [];
let obj = {};
data.forEach((item) => {
  if (!obj[item.categoryName]) {
    obj[item.categoryName] = [];
  }
  obj[item.categoryName].push(item);
});
console.log(obj); // {产品部: Array(2), 技术部: Array(2)}
for (let key in obj) {
  result.push({
    label: key,
    children: obj[key],
  });
}
console.log(result); // [{…}, {…}]
